package net.begincode.servlet.code;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import utils.BQStringUtil;
import utils.ViewCount;

import com.google.gson.Gson;

import net.begincode.bean.BcCode;
import net.begincode.bean.BcCourse;
import net.begincode.bean.BcUser;
import net.begincode.servlet.common.BaseServlet;
import net.begincode.servlet.common.ViewServlet;

public class CodeServlet extends BaseServlet{
	private static final Log LOG = LogFactory.getLog(CodeServlet.class);
	
	/**
	 * 新增代码初始化
	 * */
	public void addCodeInit(HttpServletRequest request, HttpServletResponse response){
		this.selCodeShareNav(request);//查询代码分享下导航
		try {
			
			request.getRequestDispatcher("/WEB-INF/manage/editCode.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 新增代码保存
	 * @throws UnsupportedEncodingException 
	 * */
	public void saveCode(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException{
		BcCode bcCode = new BcCode();
		//初始化表单参数
		bcCode.setBc_navigation_id(Integer.valueOf(request.getParameter("navId")));
		bcCode.setCode_info(request.getParameter("codeName"));
		bcCode.setCode_content(request.getParameter("codeContent"));
		bcCode.setCode_url(request.getParameter("codeUrl"));
		bcCode.setKeyword(request.getParameter("codeKeyWord"));
		bcCode.setCode_abstract(request.getParameter("codeAbstract"));
		//初始化其他信息
		bcCode.setCheck_flag("1");
		bcCode.setIs_begincode("1");
		bcCode.setDiscuss_flag("1");
		bcCode.setPraise(0);
		bcCode.setView_count(0);
		
		Map<String,String> userMap = this.getCookieInfo(request, response);
		bcCode.setBc_user_id(Integer.valueOf(userMap.get("bcUserId")));
		bcCode.setWriter(userMap.get("nickName"));
		
		String codeId = request.getParameter("codeId");
		
		int returnValue = 0;
		if(codeId.equals("")){
			returnValue = this.codeService.saveCode(bcCode);
		}else{
			bcCode.setBc_code_id(Integer.parseInt(codeId));
			returnValue = this.codeService.updCode(bcCode);
		}
		
		if(returnValue > 0){
			request.setAttribute("msg", "保存成功");
		}else{
			request.setAttribute("bcCode", bcCode);
			request.setAttribute("msg", "保存失败");
		}
		try {
			this.selCodeShareNav(request);//查询代码分享下导航
			request.getRequestDispatcher("/WEB-INF/manage/editCode.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	/**
	 * 查看代码
	 * */
	public void viewCode(HttpServletRequest request, HttpServletResponse response){
		String codeId = (String)request.getAttribute("id");
		ViewCount.addViewCount(ViewServlet.VIEW_CODE+":"+codeId); //添加阅读数
		request.setAttribute("bcCode", this.codeService.selCodeByIdCache(codeId));
		try {
			request.setAttribute("relchapter", this.chapterService.selNewChapterByCodeIdCache(codeId, 5));
			request.setAttribute("relProStages", this.projectStageService.selNewProStageByCodeIdCache(codeId, 5));  
			request.getRequestDispatcher("/WEB-INF/page/code_view.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	/**
	 * 查看代码列表
	 * @throws IOException 
	 * @throws ServletException 
	 * */
	public void codeList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String navId = (String)request.getAttribute("id");
		//查询导航信息
		request.setAttribute("navInfo", this.managerService.selNavByIdCache(navId));
		//查询导航下代码信息
		request.setAttribute("codes", this.codeService.selCodesByNavIdCache(navId));
		//查询最新书籍介绍
		request.setAttribute("books",this.bcDownService.selDownLimitNCache(this.BOOK, 10));
		//查询最新项目阶段
		request.setAttribute("projectStages",this.projectStageService.selNewProStageLimitNCache(10));
		request.getRequestDispatcher("/WEB-INF/page/codes_list.jsp").forward(request, response);
		
	}
 
	/**
	 * 查询共享代码
	 * @throws IOException 
	 * @throws ServletException 
	 * */
	public void selCodeByParam(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setAttribute("codes", codeService.selCodeByParam(new BcCode()));
		request.getRequestDispatcher("/WEB-INF/manage/codesManage.jsp").forward(request, response);
	}
	/**
	 * 修改代码初始化
	 * @throws IOException 
	 * @throws ServletException 
	 * */
	public void updCodeInit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		this.selCodeShareNav(request);//查询代码分享下导航
		String codeId = request.getParameter("codeId");
		request.setAttribute("bcCode", codeService.selCodeById(codeId));
		request.getRequestDispatcher("/WEB-INF/manage/editCode.jsp").forward(request, response);
	}
}
